package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSArray;
import czh.cocoa.foundation.NSMutableArray;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIScreen;
import czh.cocoa.ui.UIView;

import static czh.cocoa.ui.impl.UINavigationBar.*;
import static czh.cocoa.ui.impl.UIControl.*;
import static czh.cocoa.ui.impl.UIBarButtonItem.*;

public class UIToolbar extends UIView {
	
	private final static float PADDING = 10;
	
	private int mBarStyle;
	
	private NSArray mItems;
	
	private UIImageView mBackgroundView;
	
	private NSMutableArray mItemViews;
	
	public UIToolbar() {
		
	}
	
	protected void init() {
		super.init();
		mItemViews = new NSMutableArray(4);
		
		float bar_width = UIScreen.mainScreen().bounds().size.width;
		setFrame(CGRectMake(0, 0, bar_width, DIP(88)));
		 
		UIImageView bgview = new UIImageView(bounds());
		bgview.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
		addSubview(bgview);
		mBackgroundView = bgview;
		
		setBarStyle(UIBarStyleDefault);
	}
	
	public void setBarStyle(int style) {
		mBarStyle = style;
		UIImage backgoround;
		if (style == UIBarStyleDefault) {
			backgoround = UIImage.imageNamed(NSTR("cocoa/navi_bar_default")); 
		} else {
			backgoround = UIImage.imageNamed(NSTR("cocoa/navi_bar_black"));
		}
		mBackgroundView.setImage(backgoround);
	}
	
	public int barStyle() {
		return mBarStyle;
	}
	
	public void setItems(NSArray items) {
		mItems = items;
		
		for (NSObject o : mItemViews) {
			UIView item_view = (UIView) o;
			item_view.removeFromSuperview();
		}
		mItemViews.removeAllObjects();
		
		if (items == null) {
			return;
		}
		
		int flexible_count = 0; 
		CGSize barsize = bounds().size;
		barsize.width -= PADDING * 2;
		
		float flexible_width = barsize.width;
		for (NSObject o : items) {
			UIBarButtonItem item = (UIBarButtonItem) o;
			if (item.systemItem() != UIBarButtonSystemItemNone) {
				int systemItem = item.systemItem();
				if (systemItem == UIBarButtonSystemItemFlexibleSpace) { 
					flexible_count += 1;
				}
				UIView view = new UIView(CGRectMake(0, 0, item.width(), barsize.height)); 
				mItemViews.addObject(view);
				flexible_width -= item.width();
			} else if (item.image() != null) { 
				UIImage image = UIImage.imageWithBlendColor(item.image(), UIColor.whiteColor());
				CGSize imgsize = image.size();
				UIButton button = new UIButton(CGRectMake(0, 0, imgsize.width, imgsize.height));
				button.setImageForState(image, UIControlStateNormal);
				button.setShowsTouchWhenHighlighted(true);
				mItemViews.addObject(button);
				flexible_width -= imgsize.width;
				
				if (item.target() != null) {
					button.addTargetActionForControlEvents(item.target(), item.action(), 
							UIControlEventTouchUpInside);
				}
				
			} else if(item.title() != null) {
				NSString title = item.title();
				UIButton button = new UIButton();
				button.setShowsTouchWhenHighlighted(true);
				button.setTitleForState(title, UIControlStateNormal);
				button.setTitleColorForState(UIColor.whiteColor(), UIControlStateNormal);
				UILabel label = button.titleLabel();
				label.setFont(UIFont.boldSystemFontOfSize(23)); 
				CGSize size = label.sizeThatFits(barsize);
				button.setFrame(CGRectMake(0, 0, size.width, size.height)); 
				mItemViews.addObject(button);
				flexible_width -= size.width;
				
				if (item.target() != null) {
					button.addTargetActionForControlEvents(item.target(), item.action(), 
							UIControlEventTouchUpInside);
				}
				
			} else {
				UIView view = new UIView(CGRectMake(0, 0, item.width(), barsize.height)); 
				mItemViews.addObject(view);
				flexible_width -= item.width();
			}
		}
		
		float offx = PADDING;
		for (int i = 0; i < mItemViews.count(); i++) {
			UIView view = (UIView) mItemViews.objectAtIndex(i); 
			CGRect frame = view.frame();
			
			UIBarButtonItem item = (UIBarButtonItem) items.objectAtIndex(i);
			if (item.systemItem() == UIBarButtonSystemItemFlexibleSpace) { 
				if (flexible_width > 0) {
					frame.size.width = flexible_width / flexible_count;
				}
			}
			
			frame.origin = CGPointMake(offx, (barsize.height - frame.size.height) / 2);
			view.setFrame(frame);
			addSubview(view);
			
			offx += frame.size.width;
		}
		
	}
	
	public NSArray items() {
		return new NSArray(mItems);
	}
	
}
